/*******************************************
Module: zt_gzip.c
E-Mail: 24zhutian@gmail.com
Notices: Copyright (c) 2007-2011 ls
*******************************************/
#include "zt_config.h"
#include "zt_str.h"
#include <zlib.h>

#ifdef _MSC_VER
#pragma comment(lib,"zlibs.lib")
#endif

ZT_API int zt_ungzip(zt_str_t *dst,zt_str_t *src)
{
	int err;
	z_stream d_stream;
	
	dst->use=0;
	
	d_stream.zalloc = (alloc_func)0;
	d_stream.zfree = (free_func)0;
	d_stream.opaque = (voidpf)0;
	d_stream.next_in = src->str;
	d_stream.avail_in = 0;
	d_stream.next_out = dst->str;
	err = inflateInit2(&d_stream,47);
	if(err!=Z_OK){
		return(ZT_ERR);
	}
	while (d_stream.total_out < (dst->siz) && d_stream.total_in < src->use) {
		d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
		err = inflate(&d_stream,Z_NO_FLUSH);
		if(err == Z_STREAM_END){
			break;
		}
		if(err!=Z_OK){
			return(ZT_ERR);
		}
	}
	err = inflateEnd(&d_stream);
	if(err!=Z_OK){
		return(ZT_ERR);
	}
	dst->use=d_stream.total_out;
	
	return(ZT_OK);
}